home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / RATIONAL.H < prev    next >
C/C++ Source or Header  |  1992-03-29  |  7KB  |  262 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /* 
  4. Copyright (C) 1988 Free Software Foundation
  5.     written by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of the GNU C++ Library.  This library is free
  8. software; you can redistribute it and/or modify it under the terms of
  9. the GNU Library General Public License as published by the Free
  10. Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  This library is distributed in the hope
  12. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  13. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. PURPOSE.  See the GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. #ifndef _Rational_h
  21. #ifdef __GNUG__
  22. #pragma interface
  23. #endif
  24. #define _Rational_h 1
  25.  
  26. #include <Integer.h>
  27. #include <math.h>
  28.  
  29. class Rational
  30. {
  31. protected:
  32.   Integer          num;
  33.   Integer          den;
  34.  
  35.   void             normalize();
  36.  
  37. public:
  38.                    Rational();
  39.                    Rational(double);
  40.                    Rational(long n, long d = 1);
  41.                    Rational(const Integer& n);
  42.                    Rational(const Integer& n, const Integer& d);
  43.                    Rational(const Rational&);
  44.  
  45.                   ~Rational();
  46.  
  47.   void             operator =  (const Rational& y);
  48.  
  49.   friend int       operator == (const Rational& x, const Rational& y);
  50.   friend int       operator != (const Rational& x, const Rational& y);
  51.   friend int       operator <  (const Rational& x, const Rational& y);
  52.   friend int       operator <= (const Rational& x, const Rational& y);
  53.   friend int       operator >  (const Rational& x, const Rational& y);
  54.   friend int       operator >= (const Rational& x, const Rational& y);
  55.  
  56.   friend Rational  operator +  (const Rational& x, const Rational& y);
  57.   friend Rational  operator -  (const Rational& x, const Rational& y);
  58.   friend Rational  operator *  (const Rational& x, const Rational& y);
  59.   friend Rational  operator /  (const Rational& x, const Rational& y);
  60.  
  61.   void             operator += (const Rational& y);
  62.   void             operator -= (const Rational& y);
  63.   void             operator *= (const Rational& y);
  64.   void             operator /= (const Rational& y);
  65.  
  66. #ifdef __GNUG__
  67.   friend Rational  operator <? (const Rational& x, const Rational& y); // min
  68.   friend Rational  operator >? (const Rational& x, const Rational& y); // max
  69. #endif
  70.  
  71.   friend Rational  operator - (const Rational& x);
  72.  
  73.  
  74. // builtin Rational functions
  75.  
  76.  
  77.   void             negate();                      // x = -x
  78.   void             invert();                      // x = 1/x
  79.  
  80.   friend int       sign(const Rational& x);             // -1, 0, or +1
  81.   friend Rational  abs(const Rational& x);              // absolute value
  82.   friend Rational  sqr(const Rational& x);              // square
  83.   friend Rational  pow(const Rational& x, long y);
  84.   friend Rational  pow(const Rational& x, Integer& y);
  85.   const Integer&   numerator() const;
  86.   const Integer&   denominator() const;
  87.  
  88. // coercion & conversion
  89.  
  90.                    operator double() const;
  91.   friend Integer   floor(const Rational& x);
  92.   friend Integer   ceil(const Rational& x);
  93.   friend Integer   trunc(const Rational& x);
  94.   friend Integer   round(const Rational& x);
  95.  
  96.   friend istream&  operator >> (istream& s, Rational& y);
  97.   friend ostream&  operator << (ostream& s, const Rational& y);
  98.  
  99.  
  100. // procedural versions of operators
  101.  
  102.   friend int       compare(const Rational& x, const Rational& y);
  103.   friend void      add(const Rational& x, const Rational& y, Rational& dest);
  104.   friend void      sub(const Rational& x, const Rational& y, Rational& dest);
  105.   friend void      mul(const Rational& x, const Rational& y, Rational& dest);
  106.   friend void      div(const Rational& x, const Rational& y, Rational& dest);
  107.  
  108. // error detection
  109.  
  110.   void    error(const char* msg) const;
  111.   int              OK() const;
  112.  
  113. };
  114.  
  115. typedef Rational RatTmp; // backwards compatibility
  116.  
  117. inline Rational::Rational()  {}
  118. inline Rational::~Rational() {}
  119.  
  120. inline Rational::Rational(const Rational& y) :num(y.num), den(y.den) {}
  121.  
  122. inline Rational::Rational(const Integer& n) :num(n), den(1) {}
  123.  
  124. inline Rational::Rational(const Integer& n, const Integer& d) :num(n),den(d)
  125. {
  126.   normalize();
  127. }
  128.  
  129. inline Rational::Rational(long n, long d) :num(n), den(d)
  130. {
  131.   normalize();
  132. }
  133.  
  134. inline  void Rational::operator =  (const Rational& y)
  135. {
  136.   num = y.num;  den = y.den;
  137. }
  138.  
  139. inline int operator == (const Rational& x, const Rational& y)
  140. {
  141.   return compare(x.num, y.num) == 0 && compare(x.den, y.den) == 0;
  142. }
  143.  
  144. inline int operator != (const Rational& x, const Rational& y)
  145. {
  146.   return compare(x.num, y.num) != 0 || compare(x.den, y.den) != 0;
  147. }
  148.  
  149. inline int operator <  (const Rational& x, const Rational& y)
  150. {
  151.   return compare(x, y) <  0; 
  152. }
  153.  
  154. inline int operator <= (const Rational& x, const Rational& y)
  155. {
  156.   return compare(x, y) <= 0; 
  157. }
  158.  
  159. inline int operator >  (const Rational& x, const Rational& y)
  160. {
  161.   return compare(x, y) >  0; 
  162. }
  163.  
  164. inline int operator >= (const Rational& x, const Rational& y)
  165. {
  166.   return compare(x, y) >= 0; 
  167. }
  168.  
  169. inline int sign(const Rational& x)
  170. {
  171.   return sign(x.num);
  172. }
  173.  
  174. inline void Rational::negate()
  175. {
  176.   num.negate();
  177. }
  178.  
  179.  
  180. inline void Rational::operator += (const Rational& y) 
  181. {
  182.   add(*this, y, *this);
  183. }
  184.  
  185. inline void Rational::operator -= (const Rational& y) 
  186. {
  187.   sub(*this, y, *this);
  188. }
  189.  
  190. inline void Rational::operator *= (const Rational& y) 
  191. {
  192.   mul(*this, y, *this);
  193. }
  194.  
  195. inline void Rational::operator /= (const Rational& y) 
  196. {
  197.   div(*this, y, *this);
  198. }
  199.  
  200. inline const Integer& Rational::numerator() const { return num; }
  201. inline const Integer& Rational::denominator() const { return den; }
  202. inline Rational::operator double() const { return ratio(num, den); }
  203.  
  204. #ifdef __GNUG__
  205. inline Rational operator <? (const Rational& x, const Rational& y)
  206. {
  207.   if (compare(x, y) <= 0) return x; else return y;
  208. }
  209.  
  210. inline Rational operator >? (const Rational& x, const Rational& y)
  211. {
  212.   if (compare(x, y) >= 0) return x; else return y;
  213. }
  214. #endif
  215.  
  216. #if defined(__GNUG__) && !defined(NO_NRV)
  217.  
  218. inline Rational operator + (const Rational& x, const Rational& y) return r
  219. {
  220.   add(x, y, r);
  221. }
  222.  
  223. inline Rational operator - (const Rational& x, const Rational& y) return r
  224. {
  225.   sub(x, y, r);
  226. }
  227.  
  228. inline Rational operator * (const Rational& x, const Rational& y) return r
  229. {
  230.   mul(x, y, r);
  231. }
  232.  
  233. inline Rational operator / (const Rational& x, const Rational& y) return r
  234. {
  235.   div(x, y, r);
  236. }
  237.  
  238. #else /* NO_NRV */
  239.  
  240. inline Rational operator + (const Rational& x, const Rational& y) 
  241. {
  242.   Rational r; add(x, y, r); return r;
  243. }
  244.  
  245. inline Rational operator - (const Rational& x, const Rational& y)
  246. {
  247.   Rational r; sub(x, y, r); return r;
  248. }
  249.  
  250. inline Rational operator * (const Rational& x, const Rational& y)
  251. {
  252.   Rational r; mul(x, y, r); return r;
  253. }
  254.  
  255. inline Rational operator / (const Rational& x, const Rational& y)
  256. {
  257.   Rational r; div(x, y, r); return r;
  258. }
  259. #endif
  260.  
  261. #endif
  262.